package com.zuofei.xmpp.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.elvishew.xlog.XLog;
import com.zuofei.xmpp.dbhelper.SmsOpenHelper;

public class SmsProvider extends ContentProvider {
    private static final String AUTHORITIES = SmsProvider.class.getCanonicalName();
    public static final Uri URI_SESSION = Uri.parse("content://" + AUTHORITIES + "/session");
    static UriMatcher mUriMatcher;
    public static Uri URI_SMS = Uri.parse("content://" + AUTHORITIES + "/sms");
    public static final int SMS = 1;
    public static final int SESSION = 2;

    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //添加匹配规则
        mUriMatcher.addURI(AUTHORITIES, "/sms", SMS);
        mUriMatcher.addURI(AUTHORITIES, "/SESSION", SESSION);
    }

    private SmsOpenHelper mHelper;


    @Override
    public boolean onCreate() {
        //创建表,创建
        mHelper = new SmsOpenHelper(getContext());
        if (mHelper != null) {
            return true;
        }
        return false;
    }

    @Override
    public String getType(Uri uri) {
        switch (mUriMatcher.match(uri)) {
            case SMS:
                break;
            default:
                break;
        }
        return null;
    }


    @Override
    public Uri insert(Uri uri, ContentValues values) {
        switch (mUriMatcher.match(uri)) {
            case SMS:
                long id = mHelper.getWritableDatabase().insert(SmsOpenHelper.T_SMS, "", values);
                if (id > 0) {
                    XLog.d("----SmsProvider inssertSuccess");
                    uri = ContentUris.withAppendedId(uri, id);
                    //发送数据改变的信号
                    getContext().getContentResolver().notifyChange(SmsProvider.URI_SMS, null);
                }
                break;
            default:
                break;
        }
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int deleteCount = 0;
        switch (mUriMatcher.match(uri)) {
            case SMS:
                deleteCount = mHelper.getWritableDatabase().delete(SmsOpenHelper.T_SMS, selection, selectionArgs);
                if (deleteCount > 0) {
                    XLog.d("---------------SmsProvider 删除成功");
                    //发送数据改变的信号
                    getContext().getContentResolver().notifyChange(SmsProvider.URI_SMS, null);
                }
                break;
            default:
                break;
        }
        return deleteCount;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int updateCount = 0;
        switch (mUriMatcher.match(uri)) {
            case SMS:
                updateCount = mHelper.getWritableDatabase().update(SmsOpenHelper.T_SMS, values, selection, selectionArgs);
                if (updateCount > 0) {
                    XLog.d("-----SmsProvider 更新成功");
                    //发送数据改变的信号
                    getContext().getContentResolver().notifyChange(SmsProvider.URI_SMS, null);
                }
                break;
            default:
                break;
        }
        return updateCount;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        switch (mUriMatcher.match(uri)) {
            case SMS:
                cursor = mHelper.getReadableDatabase().query(SmsOpenHelper.T_SMS, projection, selection, selectionArgs, null, null, sortOrder);
                XLog.d("-------------------SmsProvider查询成功------------------");
                break;
            case SESSION:
                SQLiteDatabase db =  mHelper.getReadableDatabase();
                cursor = db.rawQuery("SELECT * FROM ( SELECT * from  t_sms WHERE from_account = ? or to_account = ? ORDER BY time ASC )" +
                        "GROUP BY session_account",selectionArgs);
                break;
            default:
                break;
        }
        return cursor;
    }
}
